using LitJson;
using System;
using System.IO;
using UnityEngine;

namespace LitJson
{
    public class JsonRegiter
    {
        [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
        public static void Register()
        {
            RegiterType();
            RegiterFloat();
            RegiterVertor2();
            RegiterVertor3();
            RegisterQuaternion();
            RegisterGameObject();
        }

        private static void RegiterType()
        {
            void Exporter(Type obj, JsonWriter writer)
            {
                writer.Write(obj.FullName);
            }

            JsonMapper.RegisterExporter((ExporterFunc<Type>)Exporter);

            Type Importer(string obj)
            {
                return Type.GetType(obj);
            }

            JsonMapper.RegisterImporter((ImporterFunc<string, Type>)Importer);
        }


        private static void RegiterFloat()
        {
            void Exporter(float obj, JsonWriter writer)
            {
                writer.Write(double.Parse(obj.ToString()));
            }
            JsonMapper.RegisterExporter((ExporterFunc<float>)Exporter);

            float Importer(double obj)
            {
                return (float)obj;
            }
            JsonMapper.RegisterImporter((ImporterFunc<double, float>)Importer);
        }

        private static void RegiterVertor2()
        {
            void Exporter(Vector2 v2, JsonWriter writer)
            {
                writer.WriteObjectStart();
                writer.WritePropertyName("x");
                writer.Write(v2.x);
                writer.WritePropertyName("y");
                writer.Write(v2.y);
                writer.WriteObjectEnd();
            }
            JsonMapper.RegisterExporter((ExporterFunc<Vector2>)Exporter);
        }

        private static void RegiterVertor3()
        {
            void Exporter(Vector3 v3, JsonWriter writer)
            {
                writer.WriteObjectStart();
                writer.WritePropertyName("x");
                writer.Write(v3.x);
                writer.WritePropertyName("y");
                writer.Write(v3.y);
                writer.WritePropertyName("z");
                writer.Write(v3.z);
                writer.WriteObjectEnd();
            }
            JsonMapper.RegisterExporter((ExporterFunc<Vector3>)Exporter);
        }

        private static void RegisterQuaternion()
        {
            void Exporter(Quaternion obj, JsonWriter writer)
            {
                writer.WriteObjectStart();
                writer.WritePropertyName("x");
                writer.Write(obj.x);
                writer.WritePropertyName("y");
                writer.Write(obj.y);
                writer.WritePropertyName("z");
                writer.Write(obj.z);
                writer.WritePropertyName("w");
                writer.Write(obj.w);
                writer.WriteObjectEnd();
            }
            JsonMapper.RegisterExporter((ExporterFunc<Quaternion>)Exporter);//���л�
        }


        private static void RegisterGameObject()
        {
            void Exporter(GameObject obj, JsonWriter writer)
            {
                writer.WriteObjectStart();
                writer.WritePropertyName("name");
                if (obj != null)
                    writer.Write(obj.name);
                else
                    writer.Write("PrefabΪNull");
                writer.WriteObjectEnd();
            }
            JsonMapper.RegisterExporter((ExporterFunc<GameObject>)Exporter);//���л�
        }
    }
}
public class JsonCustom 
{
    static string folder = "CustomJson";
    public static T Load<T>()
    {
        var jsonPath = $"{Application.streamingAssetsPath}/{folder}/{typeof(T)}.json";
        if (!File.Exists(jsonPath)) Save(default(T));

        var jsonTxt  = File.ReadAllText(jsonPath);
        var json     = JsonMapper.ToObject<T>(jsonTxt);

        return json;
    }

    public static void Save<T>(T target)
    {
        Debug.Log(typeof(T).Name);
        string jsonPath = $"{Application.streamingAssetsPath}/{folder}/{typeof(T)}.json";
        File.WriteAllText(jsonPath, JsonUtility.ToJson(target));
    }

    public static void Save<T>(T target,Type type)
    {
        string jsonPath = $"{Application.streamingAssetsPath}/{folder}/{type}.json";
        File.WriteAllText(jsonPath, JsonUtility.ToJson(target));
    }
}


public class JsonClass
{
    public void Save()
    {
        JsonCustom.Save(this,this.GetType());
    }

    public T Load<T>()
    {
        return JsonCustom.Load<T>();
    }
}

